package doantotnghiep.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import doantotnghiep.business.CategoryBusiness;
import doantotnghiep.model.Category;
import doantotnghiep.modelDTO.CategoryDTO;

@Controller
public class CategoryController {
	
	@Autowired
	private CategoryBusiness categoryBusiness;
	
	private CategoryDTO categoryDTO;
	
	private Category category;
	
	@ModelAttribute("CategoryDTO")
	public CategoryDTO getCategoryObject() {
		return new CategoryDTO();
	}

	@RequestMapping(value = "/viewCategory", method = RequestMethod.GET)
	public ModelAndView viewAllCategory() {
		List<Category> categories;
		List<CategoryDTO> categoryDTOs = new ArrayList<CategoryDTO>();
		if(categoryBusiness != null){
			categories = categoryBusiness.listCategory();
			
			if(categories != null){
				for (Category c : categories) {
					categoryDTO = new CategoryDTO();
					
					categoryDTO.setId(c.getId());
					categoryDTO.setCategoryName(c.getCategoryName());
					categoryDTO.setNumberMedicine(c.getNumberMedicine());
					
					categoryDTOs.add(categoryDTO);
				}
				Map<String, Object> listAllCategories = new HashMap<String, Object>();
				listAllCategories.put("categoryDTOs", categoryDTOs);
				return new ModelAndView("viewCategory", "listAllCategories",listAllCategories);
			}
		}
		return new ModelAndView("viewCategory","",null);
	}

	@RequestMapping(value = "/addCategory", method = RequestMethod.POST)
	public String addCategory(HttpServletRequest request, ModelMap model,
			@ModelAttribute("CategoryDTO") CategoryDTO categoryDTO) {
		if (categoryDTO != null) {
			category = new Category(); 
			
			category.setCategoryName(categoryDTO.getCategoryName());
			category.setNumberMedicine(categoryDTO.getNumberMedicine());
			this.categoryBusiness.addCategory(category);
		} 
		return "redirect:/viewCategory";
	}

	@RequestMapping(value = "/addCategory", method = RequestMethod.GET)
	public ModelAndView addCategoryr() {
		return new ModelAndView("addCategory","",null);
	}

	@RequestMapping(value = "/editCategory", method = RequestMethod.GET)
	public ModelAndView editCategory(HttpServletRequest request, ModelMap model,int id) {
		category = categoryBusiness.getCategoryrById(id);
		if(category != null){
			categoryDTO = new CategoryDTO();
			categoryDTO.setId(category.getId());
			categoryDTO.setCategoryName(category.getCategoryName());
			categoryDTO.setNumberMedicine(category.getNumberMedicine());
			
		   return new ModelAndView("updateCategory", "categoryDTO", categoryDTO);
		}
		return new ModelAndView("viewCategory","",null);
	}

	@RequestMapping(value = "/editCategory", method = RequestMethod.POST)
	public String editCategory(HttpServletRequest request, ModelMap model,
			@ModelAttribute("CategoryDTO") CategoryDTO categoryDTO) {
		if (categoryDTO != null) {
			category = new Category();
			category.setId(categoryDTO.getId());
			category.setCategoryName(categoryDTO.getCategoryName());
		    this.categoryBusiness.saveCategoryChanged(category);
		}
		return "redirect:/viewCategory";
	}

	@RequestMapping(value = "/deleteCategory", method = RequestMethod.GET)
	public String deleteCategory(HttpServletRequest request, ModelMap model,
			  int id) {
		categoryBusiness.deleteCategory(id);
		return "redirect:/viewCategory";
	}

}

